const querystring = require('querystring')
const http = require('http')
const url = require('url')
const fs = require('fs')
const Koa = require('koa')
const mount = require('koa-mount')

const game = require('./commonjs/lib')

let playerWon = 0

let playerLastAction = null
let sameCount = 0

const app = new Koa()

app.use(
  mount('/favicon.ico',function(ctx){
    ctx.status = 200
  })
)

const gameKoa = new Koa()
app.use(
  mount(
    '/game',
    gameKoa
  )
)
gameKoa.use(
  async function(ctx,next){
    if (playerWon >= 3 ) {
      return
    }

    await next()

    if(ctx.playerWon){
      playerWon++
    }
  }
)

gameKoa.use(
  async function(ctx,next){
    const query = ctx.query
    const playerAction = query.action
    const gameResult = game(playerAction)

    if(!playerAction){
      ctx.status = 400
      return
    }

    if(sameCount==9){
      ctx.status = 500
      ctx.body = '我不会再玩了！'
    }

    if(playerLastAction==playerAction){
      sameCount++
      if(sameCount>=3){
        ctx.status = 400
        ctx.body = '你作弊，我再也不玩了'
        sameCount = 9
        return
      }
    }else{
      sameCount = 0
    }
    playerLastAction =playerAction
    ctx.playerAction = playerAction
    await next()
  }
)

gameKoa.use(
  async function(ctx,next){
    const playerAction = ctx.playerAction
    const gameResult = game(playerAction)

    await new Promise(resolve=>{
      setTimeout(()=>{
        ctx.status = 200
        if (gameResult == 0) {
          ctx.body = '平局！'
        } else if (gameResult == -1) {
          ctx.body = '你输了！'
        } else {
          ctx.body = '你赢了！'
          ctx.playerWon = true
        }
        resolve()
      },500)
    })
  }
)

app.use(
  mount('/',function(ctx){
    ctx.body = fs.readFileSync(__dirname + '/index.html','utf-8')
  })
)

app.listen(8888)

// http
//   .createServer((request, response) => {
//     const parseUrl = url.parse(request.url)

//     if (request.url == '/favicon.ico') {
//       response.writeHead(200)
//       response.end()
//       return
//     }

//     if (parseUrl.pathname == '/game') {
//       const query = querystring.parse(parseUrl.query)
//       const playerAction = query.action
//       const gameResult = game(playerAction)

//       if (playerWon >= 3 || sameCount == 9) {
//         response.writeHead(500)
//         response.end('我再也不和你玩了！')
//         return
//       }

//       if (playerLastAction && playerAction == playerLastAction) {
//         sameCount++
//       } else {
//         sameCount = 0
//       }

//       playerLastAction = playerAction

//       if (sameCount >= 3) {
//         response.writeHead(400)
//         response.end('你作弊了！')
//         sameCount = 9
//         return
//       }

//       response.writeHead(200)

//       if (gameResult == 0) {
//         response.end('平局！')
//       } else if (gameResult == 1) {
//         playerWon++
//         response.end('你赢了！')
//       } else {
//         response.end('你输了！')
//       }
//       response.end()
//     }

//     if (parseUrl.pathname == '/') {
//       fs.createReadStream(__dirname + '/index.html').pipe(response)
//     }
//   })
//   .listen(8888)
console.log('Server running at http://127.0.0.1:8888/')
